﻿using System.Linq;
using EasyIot.Model;

namespace EasyIot.Service
{
    public class RoleService : BaseService<RoleModel>
    {
        public static dynamic GetListByFilter(RoleModel filter, PageInfo pageInfo)
        {
            pageInfo.prefix = "a.";
            string _where = " role a INNER JOIN itemsdetail b ON a.TypeClass=b.Id";
            if (!string.IsNullOrEmpty(filter.EnCode))
            {
                _where += string.Format(" and {0}EnCode like concat('%',@EnCode,'%')", pageInfo.prefix);
            }
            if (!string.IsNullOrEmpty(filter.FullName))
            {
                _where += string.Format(" and {0}FullName like concat('%',@FullName,'%')", pageInfo.prefix);
            }
            pageInfo.returnFields = string.Format("{0}Id,{0}EnCode,{0}FullName,b.ItemName as TypeName,{0}SortCode,{0}SystemSign,{0}CreateTime", pageInfo.prefix);
            return GetPageUnite(filter, pageInfo, _where);
        }

        public static bool IsExistCode(string encode)
        {
            string where = "where EnCode=@EnCode";
            return GetByWhere(where, new { EnCode = encode }).Count() > 0 ? true : false;
        }

        public static bool DeleteByIds(string[] idsArray)
        {
            return DeleteByWhere("where Id in @Ids and SystemSign=0", new { Ids = idsArray });
        }
    }
}
